package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.qo.CustomerReportQuery;
import cn.wolfcode.crm.service.ICustomerReportService;
import cn.wolfcode.crm.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

    @Autowired
    private ICustomerReportService customerReportService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerReportQuery qo) {
        PageInfo pageInfo = customerReportService.selectCustomerResport(qo);
        model.addAttribute("result", pageInfo);
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "/customerReport/table";
    }

    @RequestMapping("/listForBar")
    public String listForBar(Model model, @ModelAttribute("qo") CustomerReportQuery qo) {
        //获取全部数据 (不分页)
        List<Map> list=customerReportService.listAll();

        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储y轴的数据
        ArrayList yList = new ArrayList();
        for (Map map : list) {
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }
        //先把数据 转成Json字符串 才能给echart 插件识别
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList", JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "/customerReport/listForBar";
    }

    @RequestMapping("/listForPie")
    public String listForPie(Model model, @ModelAttribute("qo") CustomerReportQuery qo) {
        //获取全部数据 (不分页)
        List<Map> list=customerReportService.listAll();

        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储y轴的数据
        ArrayList yList = new ArrayList();
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<Object, Object> temp = new HashMap<>();
            temp.put("name", map.get("groupType"));
            temp.put("value", map.get("number"));
            yList.add(temp);
        }
        //先把数据 转成Json字符串 才能给echart 插件识别
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList", JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "/customerReport/listForPie";
    }
}
